package main

import (
	"database/sql"
	"log"
)

// P2PCall 数据库实体
type P2PCall struct {
	CallId    int    `json:"callId" db:"call_id"`
	Caller    int    `json:"caller" db:"caller"`
	Receiver  int    `json:"receiver" db:"receiver"`
	Type      string `json:"type" db:"type"`
	StartTime string `json:"startTime" db:"start_time"`
	EndTime   string `json:"endTime" db:"end_time"`
}

// ApplyP2PCall 申请通话对象
type ApplyP2PCall struct {
	UserId       int    `json:"userId"`
	Nickname     string `json:"nickname"`
	Avatar       string `json:"avatar"`
	TargetId     int    `json:"targetId"`
	TargetName   string `json:"targetName"`
	TargetAvatar string `json:"targetAvatar"`
	CallType     string `json:"callType"`
}

// ResponseP2PCall 响应申请通话对象
type ResponseP2PCall struct {
	UserId     int    `json:"userId"`   // 接收方
	TargetId   int    `json:"targetId"` // 发起方
	RespStatus bool   `json:"respStatus"`
	Reason     string `json:"reason"`
}

// IceP2PCall Stream和 Ice对象
type IceP2PCall struct {
	TargetId  int         `json:"targetId"`
	Candidate interface{} `json:"candidate"`
}

// DescriptionP2PCall 发送offer/answer
type DescriptionP2PCall struct {
	TargetId int         `json:"targetId"`
	Desc     interface{} `json:"desc"`
}

type NotifyFrameBuilt struct {
	TargetId int `json:"targetId"`
}

// RecordCall 记录通话
func RecordCall(call P2PCall) {
	db, err := sql.Open("sqlite3", "./RealTimeCommunication.db")
	if err != nil {
		log.Fatal("数据库打开失败:", err)
		return
	}
	defer db.Close()

	stmt, err := db.Prepare("INSERT INTO p2p_call (caller, receiver, type, start_time, end_time) VALUES (?, ?, ?, ?, ?)")
	if err != nil {
		log.Println("数据库异常", err)
		return
	}
	defer stmt.Close()

	_, err = stmt.Exec(call.Caller, call.Receiver, call.Type, call.StartTime, call.EndTime)
	if err != nil {
		log.Fatal("数据库异常", err)
		return
	}
	return
}
